package com.vitabuybuy.tv.ui.mine

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityUserSignBinding
import com.vitabuybuy.tv.ui.mine.adapter.DateAdapter
import com.vitabuybuy.tv.ui.mine.adapter.PointRewardsAdapter2
import com.vitabuybuy.tv.ui.mine.viewmodel.MineViewModel
import com.vitabuybuy.tv.utils.DateUtils

class UserSignActivity : BaseActivity<MineViewModel, ActivityUserSignBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_user_sign

    private var dateAdapter: DateAdapter? = null
    private var mAdapter: PointRewardsAdapter2? = null
    private var year = 0
    private var month = 0
    private var day = 0
    private var days = Array(6) { IntArray(7) }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.sign_in))
        mBinding.lvActivityPoint.apply {
            layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)
        }
        year = DateUtils.getYear()
        month = DateUtils.getMonth()
        day = DateUtils.getCurrentDayOfMonth()
        title = year.toString().plus("-").plus(month.toString())
        mBinding.recordTitle.text = title
        days = DateUtils.getDayOfMonthFormat(year, month)
        dateAdapter = DateAdapter(this, days, day) //传入当前月的年
        mBinding.recordGridView.apply {
            adapter = dateAdapter
            isEnabled = false
        }
    }

    override fun observeData() {
        viewModel.isSign()
        viewModel.userSign.observe(this, Observer {
            mAdapter = PointRewardsAdapter2(it.quantity)
            mAdapter?.setNewData(it.activity.activityNodeList)
            mBinding.lvActivityPoint.adapter = mAdapter

            mBinding.tvAlreadySignDays.text =
                String.format(getString(R.string.already_sign_days), it.quantity)
            mBinding.btnSignUp.isEnabled = !it.sign
            if (it.sign) {
                mBinding.btnSignUp.text = getString(R.string.already_sign_in)
            } else {
                mBinding.btnSignUp.text = getString(R.string.sign_in)
            }
        })
    }

    override fun eventHandling() {
        mBinding.btnSignUp.setOnClickListener {
            viewModel.userSign()
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                viewModel.isSign()
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, UserSignActivity::class.java)
        }
    }
}
